<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class JFX_Module_Blockstyle_Element_Base
{
	public $id = '.jfx-blockstyle-base';
	public $properties = array();
	public $enabled = false;
	public $title = 'Default: give this element a title!';
	public $tagname = '';
	public $privateProperties = array(
		'properties',
		'privateProperties'
	);
	public $runtimeProperties = array();
	public $group = '';
	
	public function  __construct() {
		// all elements inherit these properties
		$this->init();
	}

	public function init(){
		if($this->enabled<=0){
			return;
		}

		$background = new JFX_Module_Blockstyle_Property_Background($this, $this->id.'-background-0');
		if(!$this->hasProperty($background->instanceId)){
			$this->properties[] = $background;
		}
		
		$border = new JFX_Module_Blockstyle_Property_Border($this, $this->id.'-border-0');
		if(!$this->hasProperty($border->instanceId)){
			$this->properties[] = $border;
		}

		$margin = new JFX_Module_Blockstyle_Property_Margin($this, $this->id.'-margin-0');
		if(!$this->hasProperty($margin->instanceId)){
			$this->properties[] = $margin;
		}

		$padding = new JFX_Module_Blockstyle_Property_Padding($this, $this->id.'-padding-0');
		if(!$this->hasProperty($padding->instanceId)){
			$this->properties[] = $padding;
		}

		$properties = new JFX_Module_Blockstyle_Property_Properties($this, $this->id.'-properties-0');
		if(!$this->hasProperty($properties->instanceId)){
			$this->properties[] = $properties;
		}
	}

	public function hasEnabledWidget(){
		foreach($this->properties as $k=>$property){
			if($property->hasEnabledWidget()){
				return true;
			}
		}
		return false;
	}

	public function __toCss($classname){
		$outputString = '';

		if(!$this->enabled || $this->tagname==''){
			return $outputString;
		}

		if(count($this->properties)>0){

			// gonna calculate the dom selector
			if($this->tagname != $this->id){
				switch($this->id){
					case 'block':
					case 'properties':
						// this is the container element
						$outputString .= '.'.$classname.' {
';
					break;
					case 'ahover':
						$outputString .= '.'.$classname.' a:hover {
';
					break;
					default:
						die(__FILE__.__LINE__.$this->id.' '.$this->tagname);
					break;
				}
			}else{
				$outputString .= '.'.$classname.' '.$this->id.' {
';
			}

			

			foreach($this->properties as $classname=>$property){
				if(!is_object($property) || false==($property instanceof JFX_Module_Blockstyle_Property_Base)){
					continue;
				}
				$outputString .= $property->__toCss();
			}

			$outputString .= '}
';

		}

		return $outputString;
	}

	public function updateFromJson($element){
		foreach($element->properties as $id=>$property){
			if($this->hasProperty($property->instanceId)){
				$this->getProperty($property->instanceId)->updateFromJson($property);
			}
		}
		foreach(get_object_vars($element) as $k=>$v){
			if(isset($this->$k)){
				if(in_array($k, $this->privateProperties)){
					continue;
				}
				$this->$k = $v;
			}
		}
	}


	public function hasProperty($propertyInstanceId){
		$property = $this->getProperty($propertyInstanceId);
		if(is_object($property) && $property instanceof JFX_Module_Blockstyle_Property_Base){
			return true;
		}
		return false;
	}

	public function getProperty($propertyInstanceId){
		foreach($this->properties as $k=>$property){
			if($propertyInstanceId == $property->instanceId){
				return $this->properties[$k];
			}
		}
	}

	public function setWidgetProperty($widgetInstanceId, $name, $value){
		if($this->hasWidget($widgetInstanceId)){
			$this->getWidget($widgetInstanceId)->setProperty($name, $value);
		}
	}

	public function hasWidget($widgetInstanceId){
		$widget = $this->getWidget($widgetInstanceId);
		if(is_object($widget) && $widget instanceof JFX_Module_Blockstyle_Widget_Base){
			return true;
		}
		return false;
	}

	public function removeWidget($widgetInstanceId){
		if($this->hasWidget($widgetInstanceId)){
			foreach($this->properties as $k=>$property){
				$this->properties[$k]->removeWidget($widgetInstanceId);
			}
		}
	}

	public function removeProperty($propertyInstanceId){
		if($this->hasProperty($propertyInstanceId)){
			foreach($this->properties as $k=>$property){
				if($property->instanceId == $propertyInstanceId){
					unset($this->properties[$k]);
				}
			}
			$this->properties = array_regenerate_keys($this->properties);
		}
	}

	public function getWidget($widgetInstanceId){
		foreach($this->properties as $k=>$property){
			if($property->hasWidget($widgetInstanceId)){
				return $this->properties[$k]->getWidget($widgetInstanceId);
			}
		}
	}

	public function prepareJson(){
		$this->init();
		foreach($this->runtimeProperties as $propertyName){
			if(isset($this->$propertyName)){
				$this->propertyName = null;
			}
		}
		foreach($this->properties as $k=>$property){
			$this->properties[$k]->prepareJson();
		}
	}

	public function __toJson(){
		$this->prepareJson();
		return json_encode($this);
	}

	
}





